package com.hknc.btscale.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hknc.btscale.R;
import com.hknc.btscale.bean.TejiaBean;
import com.hknc.btscale.bean.TodayCheapBean;

import java.util.List;

public class TodayCheapAdapter extends RecyclerView.Adapter<TodayCheapAdapter.MyViewHolder> {
    private List<TodayCheapBean.DataBean> mTejiaBeanList;
    private Context mContext;
    private int resourceId;

    public TodayCheapAdapter(List<TodayCheapBean.DataBean> tejiaBeanList, Context context, int resourceId) {
        mTejiaBeanList = tejiaBeanList;
        mContext = context;
        this.resourceId = resourceId;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(resourceId, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String priceTemp = String.valueOf(mTejiaBeanList.get(position).getGoodsCurrentPrice());
        String price1 = "";
        String price2 = "";
        if (!TextUtils.isEmpty(priceTemp) && priceTemp.length() > 0) {
            price1 = priceTemp.substring(0, priceTemp.indexOf("."));
            price2 = priceTemp.substring(priceTemp.indexOf("."));

            holder.price.setText(price1);
            holder.price2.setText(price2);
        }
        holder.goosName.setText(mTejiaBeanList.get(position).getGoodsName());
        holder.unit.setText("元/" + mTejiaBeanList.get(position).getUnit());
    }

    @Override
    public int getItemCount() {
        return mTejiaBeanList == null ? 0 : mTejiaBeanList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView goosName, price, unit, price2;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            goosName = itemView.findViewById(R.id.goodsname_tv);
            price = itemView.findViewById(R.id.price_tv);
            unit = itemView.findViewById(R.id.unit_tv);
            price2 = itemView.findViewById(R.id.price_tv1);
        }
    }
}
